package com.java110.mall.goods.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.dto.asset.AssetInventoryDto;
import com.java110.mall.goods.bmo.assetInventory.IDeleteAssetInventoryBMO;
import com.java110.mall.goods.bmo.assetInventory.IGetAssetInventoryBMO;
import com.java110.mall.goods.bmo.assetInventory.IUpdateAssetInventoryBMO;
import com.java110.po.assetInventory.AssetInventoryPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/assetInventory")
public class AssetInventoryApi {

    @Autowired
    private IUpdateAssetInventoryBMO updateAssetInventoryBMOImpl;

    @Autowired
    private IDeleteAssetInventoryBMO deleteAssetInventoryBMOImpl;

    @Autowired
    private IGetAssetInventoryBMO getAssetInventoryBMOImpl;

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /assetInventory/updateAssetInventory
     * @path /app/assetInventory/updateAssetInventory
     */
    @RequestMapping(value = "/updateAssetInventory", method = RequestMethod.POST)
    public ResponseEntity<String> updateAssetInventory(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含商铺");
        Assert.hasKeyAndValue(reqJson, "aiId", "aiId不能为空");

        if (!reqJson.containsKey("inoutOrderProducts")) {
            throw new IllegalArgumentException("未包含盘点商品");
        }

        JSONArray inoutOrderProducts = reqJson.getJSONArray("inoutOrderProducts");

        if (inoutOrderProducts == null || inoutOrderProducts.size() < 1) {
            throw new IllegalArgumentException("未包含盘点商品");
        }


        AssetInventoryPo assetInventoryPo = BeanConvertUtil.covertBean(reqJson, AssetInventoryPo.class);
        return updateAssetInventoryBMOImpl.update(assetInventoryPo, inoutOrderProducts);
    }

    /**
     * 入库
     *
     * @param reqJson
     * @return
     * @serviceCode /assetInventory/assetInventoryInStock
     * @path /app/assetInventory/assetInventoryInStock
     */
    @RequestMapping(value = "/assetInventoryInStock", method = RequestMethod.POST)
    public ResponseEntity<String> assetInventoryInStock(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "shopId", "请求报文中未包含商铺");
        Assert.hasKeyAndValue(reqJson, "aiId", "aiId不能为空");
        Assert.hasKeyAndValue(reqJson, "products", "aiId不能为空");

        if (!reqJson.containsKey("products")) {
            throw new IllegalArgumentException("未包含盘点商品");
        }

        JSONArray products = reqJson.getJSONArray("products");

        if (products == null || products.size() < 1) {
            throw new IllegalArgumentException("未包含盘点商品");
        }


        AssetInventoryPo assetInventoryPo = BeanConvertUtil.covertBean(reqJson, AssetInventoryPo.class);
        return updateAssetInventoryBMOImpl.inStock(assetInventoryPo, products);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /assetInventory/deleteAssetInventory
     * @path /app/assetInventory/deleteAssetInventory
     */
    @RequestMapping(value = "/deleteAssetInventory", method = RequestMethod.POST)
    public ResponseEntity<String> deleteAssetInventory(@RequestBody JSONObject reqJson) {
//        Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");

        Assert.hasKeyAndValue(reqJson, "aiId", "aiId不能为空");


        AssetInventoryPo assetInventoryPo = BeanConvertUtil.covertBean(reqJson, AssetInventoryPo.class);
        return deleteAssetInventoryBMOImpl.delete(assetInventoryPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param shopId 小区ID
     * @return
     * @serviceCode /assetInventory/queryAssetInventory
     * @path /app/assetInventory/queryAssetInventory
     */
    @RequestMapping(value = "/queryAssetInventory", method = RequestMethod.GET)
    public ResponseEntity<String> queryAssetInventory(@RequestParam(value = "shopId") String shopId,
                                                      @RequestParam(value = "shId", required = false) String shId,
                                                      @RequestParam(value = "staffName", required = false) String staffName,
                                                      @RequestParam(value = "state", required = false) String state,
                                                      @RequestParam(value = "aiId", required = false) String aiId,
                                                      @RequestParam(value = "page") int page,
                                                      @RequestParam(value = "row") int row) {
        AssetInventoryDto assetInventoryDto = new AssetInventoryDto();
        assetInventoryDto.setPage(page);
        assetInventoryDto.setRow(row);
        assetInventoryDto.setShopId(shopId);
        assetInventoryDto.setShId(shId);
        assetInventoryDto.setStaffName(staffName);
        assetInventoryDto.setState(state);
        assetInventoryDto.setAiId(aiId);
        return getAssetInventoryBMOImpl.get(assetInventoryDto);
    }
}
